package com.ccx.coachsite.bus;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.MainSplash;
import com.ccx.coachsite.R;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.http.HttpCallBack;
import com.ccx.coachsite.http.HttpRequest;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.service.ShareMessageService;
import com.ccx.coachsite.task.TaskBase;
import com.ccx.coachsite.task.TaskType;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.login.RegisterActivity;
import com.ccx.coachsite.util.Log;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class TianyiLoginBus extends BaseBus{
	private boolean isShowProcess;
	private String mMobile;
	public boolean isShowProcess() {
		return isShowProcess;
	}

	public void setShowProcess(boolean isShowProcess) {
		this.isShowProcess = isShowProcess;
	}
	
	public TianyiLoginBus(Activity activity) {
		super(activity, activity.getString(R.string.login_process));
		isShowProcess = true;
		successInfo = "登录成功";
		errorInfo = "登录失败";
		mMobile = "";
	}
	
	public void onCompleted(boolean isSuccess, String desc) {
		if (isShowProcess) {
			dismissProgress();
		}
		//if (isSuccess) 
		if(!isSuccess)
		{
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_UserId_Key, "");
		}
		
			
			//启动分享推送检测服务
			mContext.startService(new Intent(mContext,ShareMessageService.class));
			
			if (mContext instanceof MainSplash) {
				Intent intent = new Intent(mContext,
						CoachSiteMainActivity.class);
				mContext.startActivity(intent);
			}
			if (mActivity != null)
				mActivity.finish();
		
	}
	
	public void doTianyiLogin(boolean isShowProcess)
	{
		this.isShowProcess = isShowProcess;
		TianyiLoginTask tianTask = new TianyiLoginTask(this, mActivity);
		String Imsi = LoginHelper.getMobileIMSI(mActivity);
		tianTask.setParams(Imsi);
		tianTask.execute(0);
		
		/*
		if(isShowProcess)
		{
			TianyiLoginTask tianTask = new TianyiLoginTask(this, mActivity);
			String Imsi = LoginHelper.getMobileIMSI(mActivity);
			tianTask.setParams(Imsi);
			tianTask.execute(0);
		}
		else
		{
			String mobile = SharedPreferencesUtil.getValue(mContext, SharedKey.Shared_Mobile_Key);
			LoadDataBus loadData = new LoadDataBus(mContext, this);
			loadData.loadData(mobile);
		}
		*/
	}
	
	private void doLoginComplete(int state, String desc) {
		dismissProgress();

		if(isShowProcess)
		{
			String toaskInfo = desc;
			Toast.makeText(mContext, toaskInfo, Toast.LENGTH_SHORT).show();
		}
		
		if (!isShowProcess) {
			// Toast.makeText(mContext, "登陆失败", Toast.LENGTH_SHORT).show();
			SharedPreferencesUtil.setValue(mContext,
					SharedKey.Shared_UserId_Key, "");
			Intent intent = new Intent(mActivity, CoachSiteMainActivity.class);
			mActivity.startActivity(intent);
			mActivity.finish();
		}
		else if(state == 0)
		{
			Intent intent = new Intent(mContext, RegisterActivity.class);
			mContext.startActivity(intent);
		}
		else if(state == 1)
		{
			Intent intent=new Intent(mContext,RegisterActivity.class);
			intent.putExtra("modifytype",3);
			intent.putExtra("loginType",4);
			intent.putExtra("mobile",mMobile);
			mContext.startActivity(intent);
		}
			
	}
	
	@Override
	public void callBack(int taskType, BaseResp response, String responseJson) {
		int loginState = 0;
		String loginDesc = "无法获取您的手机信息，请先注册。";
		if (response.isSuccess()) {
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(responseJson);
				JSONObject respsonObject = jsonObject.getJSONObject("response");
				if(respsonObject != null)
				{
					mMobile = respsonObject.getString("mobile");
					String userId = "";
					if(!respsonObject.isNull("userId"))
						userId = respsonObject.getInt("userId") + "";
					//String userType = respsonObject.getString("userType");
					String userType = "1"; // respsonObject.getString("userType");
					if(!respsonObject.isNull("userType"))
						userType = respsonObject.getString("userType");
					if(!StringUtil.isEmpty(mMobile) && !mMobile.equals("null"))
					{
						if(!StringUtil.isEmpty(userId))
						{
							SharedPreferencesUtil.setValue(mContext,
									SharedKey.Shared_Mobile_Key, mMobile);
							SharedPreferencesUtil.setValue(mContext,
									SharedKey.Shared_UserId_Key, userId);
							
							boolean isExpert = false;
							if(userType.equals("0"))
								isExpert = true;
							
							SharedPreferencesUtil.setBoolean(mContext, SharedKey.Shared_Is_Expert, isExpert);
							SharedPreferencesUtil.setValue(mContext,
									SharedKey.Shared_Login_Type, "4");
							
							if(userId != null && !userId.equals(""))
							{
								LoginHelper.doLoginStatic(mContext, mMobile, userId);
							}
							
							LoadDataBus loadData = new LoadDataBus(mContext, this);
							loadData.loadData(mMobile);
							loginState = 2;
							loginDesc = successInfo;
						}
						else
						{
							loginState = 1;
							loginDesc = "您的天翼手机还未注册过，请先注册。";
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} 
		/*
		mMobile = "15311111111";
		String userId = "";
		String userType = "0";
		if(!StringUtil.isEmpty(mMobile))
		{
			if(!StringUtil.isEmpty(userId))
			{
				SharedPreferencesUtil.setValue(mContext,
						SharedKey.Shared_Mobile_Key, mMobile);
				SharedPreferencesUtil.setValue(mContext,
						SharedKey.Shared_UserId_Key, userId);
				
				boolean isExpert = false;
				if(userType.equals("0"))
					isExpert = true;
				
				SharedPreferencesUtil.setBoolean(mContext, SharedKey.Shared_Is_Expert, isExpert);
				SharedPreferencesUtil.setValue(mContext,
						SharedKey.Shared_Login_Type, "4");
				
				LoadDataBus loadData = new LoadDataBus(mContext, this);
				loadData.loadData(mMobile);
				loginState = 2;
				loginDesc = successInfo;
			}
			else
			{
				loginState = 1;
				loginDesc = "您的天翼手机还未注册过，请先注册。";
			}
		}
		*/
		
		if (!response.isSuccess()) {
			if(!StringUtil.isEmpty(response.getErrorCode()) && response.getErrorCode().equals("231"))
			{
				loginDesc = "帐号已停用";
			    loginState = 3;
			}
		}
		if(loginState != 2)
		{
			doLoginComplete(loginState, loginDesc);
		}
	}
	
	class TianyiLoginTask extends TaskBase {
		public TianyiLoginTask(HttpCallBack<?> iCall, Context context) {
			super(iCall, context);
		}

		private String imsi;
		public void setParams(String imsi) {
			this.imsi = imsi;
		}
		
		@Override
		protected String genUrl(HttpRequest request)
		{
			String baseUrl = UrlConfig.getUrlByKey(UrlConfig.TianyiLoginUrlKey);
			baseUrl = baseUrl + "/" + imsi;
			//Log.e(baseUrl);
			return baseUrl;
		}
	}
}
